<?php

class Admin_BannersController extends Zend_Controller_Action {

    private $table = null;

    public function init() {
        $this->table = new Jhg_Model_DbTable_Banners();
    }

    public function indexAction() {
        
    }

    public function listAction() {
        $res = $this->table->getList();
        $this->_helper->json($res->toArray());
    }

    public function singleAction() {
        $res = $this->table->find($this->_getParam('id'))->current();
        $data = $res->toArray();
        $data['categories'] = trim($data['categories'], '|');
        if ($data['categories'] === '') {
            $data['categories'] = array();
        } else {
            $data['categories'] = explode('|', $data['categories']);
        }
        $this->_helper->json($data);
    }

    public function saveAction() {
        $data = (array) $this->_getParam('data');
        $id = (int) $data['id'];
        unset($data['id']);
        $data['categories'] = count($data['categories']) ? '|' . join('|', $data['categories']) . '|' : '';
        $row = ($id === -1) ? $this->table->fetchNew() : $this->table->find($id)->current();
        $row->setFromArray($data);
        $res = $row->save();
        $this->_helper->json($res);
    }

    public function removeAction() {
        $id = (int) $this->_getParam('id');
        $row = $this->table->find($id)->current();
        $res = $row->delete();
        $this->_helper->json($res);
    }

//    public function categoriesAction() {
//        $categories = new Content_Model_DbTable_Categories();
//        $res = $categories->getShortList();
//        $this->_helper->json($res->toArray());
//    }
//    
//    private function modifiedText($text) {
//        require 'Markdown/markdown.php';
//        return Markdown($text);
//    }
}

